<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace common\helper;

use common\models\AppLogs;
use common\models\EmailRecord;
use common\models\SmsRecord;
use Yii;

/**
 * 应用
 * @author cleverstone
 * @since ym2.0
 */
class App extends \builder\helper\App
{
    /**
     * 记录调试日志
     * @param string $category 种类
     * @param array $message 消息
     * @param string $businessType 业务类型
     * @param int $businessID 业务ID
     */
    public static function logDebug($category, array $message, $businessType = 'other', $businessID = 0)
    {
        if (YII_DEBUG) {
            static::log('DEBUG', $category, $message, $businessType, $businessID);
        }
    }

    /**
     * 记录信息日志
     * @param string $category 种类
     * @param array $message 消息
     * @param string $businessType 业务类型
     * @param int $businessID 业务ID
     */
    public static function logInfo($category, array $message, $businessType = 'other', $businessID = 0)
    {
        static::log('INFO', $category, $message, $businessType, $businessID);
    }

    /**
     * 记录警告日志
     * @param string $category 种类
     * @param array $message 消息
     * @param string $businessType 业务类型
     * @param int $businessID 业务ID
     */
    public static function logWarn($category, array $message, $businessType = 'other', $businessID = 0)
    {
        static::log('WARNING', $category, $message, $businessType, $businessID);
    }

    /**
     * 记录错误日志
     * @param string $category 种类
     * @param array $message 消息
     * @param string $businessType 业务类型
     * @param int $businessID 业务ID
     */
    public static function logError($category, array $message, $businessType = 'other', $businessID = 0)
    {
        static::log('ERROR', $category, $message, $businessType, $businessID);
    }

    /**
     * 记录应用日志
     * @param string $level 级别 DEBUG、INFO、WARNING、ERROR
     * @param string $category 种类
     * @param array $message 消息
     * @param string $businessType 业务类型
     * @param int $businessID 业务ID
     */
    public static function log($level, $category, $message, $businessType, $businessID)
    {
        if (PHP_SAPI == 'cli') {
            $prefix = '[ CLI ] [ - ] [ ' . PHP_OS . ' ]';
        } else {
            $userClient = Http::getClientInfo();
            $prefix = '[ CGI-FCGI ] [ '
                . (Yii::$app->request->userIP ?: '-') . ' ] [ '
                . $userClient->platform() . ' ] [ '
                . $userClient->browser() . " {$userClient->browserVersion()}"
                . ' ]';
        }

        $model = new AppLogs();
        $model->setScenario('SCE_Log_Create');
        $model->setAttributes([
            'level' => $level,
            'category' => $category,
            'prefix' => $prefix,
            'message' => var_export($message, true),
            'business_type' => $businessType,
            'business_id' => $businessID,
        ]);
        if (!$model->save()) {
            Yii::error($model->error, __METHOD__);
        }
    }

    /**
     * 校验邮箱验证码
     * @param string $receiveEmail 接收邮箱
     * @param string $code 待校验码
     * @return bool
     */
    public static function verifyEmailCode($receiveEmail, $code)
    {
        $data = EmailRecord::query(['code', 'created_at'])
            ->where(['receive_email' => $receiveEmail])
            ->orderBy(['id' => SORT_DESC])
            ->one();
        if (!empty($data)) {
            $createTime = strtotime($data['created_at']);
            if (time() - $createTime < 300 && !strcmp($data['code'], $code)) {
                return true;
            }
        }

        return false;
    }

    /**
     * 校验短信验证码
     * @param string $receiveMobile 接收手机号
     * @param string $code 待校验码
     * @return bool
     */
    public static function verifySmsCode($receiveMobile, $code)
    {
        $data = SmsRecord::query(['code', 'created_at'])
            ->where(['receive_email' => $receiveMobile])
            ->orderBy(['id' => SORT_DESC])
            ->one();
        if (!empty($data)) {
            $createTime = strtotime($data['created_at']);
            if (time() - $createTime < 300 && !strcmp($data['code'], $code)) {
                return true;
            }
        }

        return false;
    }
}